package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.system.domain.SysStore;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysStoreService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店Controller
 * 
 * @author ruoyi
 * @date 2024-12-26
 */
@Controller
@RequestMapping("/system/store")
public class SysStoreController extends BaseController
{
    private String prefix = "system/store";

    @Autowired
    private ISysStoreService sysStoreService;

    @RequiresPermissions("system:store:view")
    @GetMapping()
    public String store()
    {
        return prefix + "/store";
    }

    /**
     * 查询门店列表
     */
    @RequiresPermissions("system:store:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStore sysStore)
    {
        startPage();
        List<SysStore> list = sysStoreService.selectSysStoreList(sysStore);
        return getDataTable(list);
    }
    /**
     * 加载部门列表树
     */
    @GetMapping("/storeTreeData")
    @ResponseBody
    public List<Ztree> storeTreeData()
    {
        List<Ztree> ztrees = sysStoreService.selectStoreTree(new SysStore());
        return ztrees;
    }

    /**
     * 选择部门树
     *
     * @param storeId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectStoreTree/{storeId}", "/selectStoreTree/{storeId}/{excludeId}" })
    public String selectStoreTree(@PathVariable("storeId") Long storeId,
                                 @PathVariable(value = "excludeId", required = false) Long excludeId, ModelMap mmap)
    {
        mmap.put("store", sysStoreService.selectSysStoreByStoreId(storeId));
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }
    /**
     * 导出门店列表
     */
    @RequiresPermissions("system:store:export")
    @Log(title = "门店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStore sysStore)
    {
        List<SysStore> list = sysStoreService.selectSysStoreList(sysStore);
        ExcelUtil<SysStore> util = new ExcelUtil<SysStore>(SysStore.class);
        return util.exportExcel(list, "门店数据");
    }

    /**
     * 新增门店
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存门店
     */
    @RequiresPermissions("system:store:add")
    @Log(title = "门店", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStore sysStore)
    {
        return toAjax(sysStoreService.insertSysStore(sysStore));
    }

    /**
     * 修改门店
     */
    @RequiresPermissions("system:store:edit")
    @GetMapping("/edit/{storeId}")
    public String edit(@PathVariable("storeId") Long storeId, ModelMap mmap)
    {
        SysStore sysStore = sysStoreService.selectSysStoreByStoreId(storeId);
        mmap.put("sysStore", sysStore);
        return prefix + "/edit";
    }

    /**
     * 修改保存门店
     */
    @RequiresPermissions("system:store:edit")
    @Log(title = "门店", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStore sysStore)
    {
        return toAjax(sysStoreService.updateSysStore(sysStore));
    }

    /**
     * 删除门店
     */
    @RequiresPermissions("system:store:remove")
    @Log(title = "门店", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStoreService.deleteSysStoreByStoreIds(ids));
    }
}
